package com.nf.service;

import com.nf.dao.AccountMapper;
import com.nf.dao.RecordMapper;
import com.nf.entity.Account;
import com.nf.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AccountServiceimpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RecordMapper recordMapper;

    @Transactional
    //注册
    public Account addAccount(Account account) {
            accountMapper.addAccount(account);
        return account;
    }
    @Transactional
    public Account login(String userName, String password) {
        Account acount = accountMapper.getAcountUserName(userName);
        if(acount==null){
            return null;
        }else if(password.equals(acount.getPassWord())){
            return acount;
        }else
        {
            return null;
        }
    }

    @Transactional
    public Integer queryBanlanceById(Integer userid) {
        Account account = accountMapper.getAccount(userid);
        if (account==null){
            return null;
        }
        return null;
    }

    @Transactional
    public boolean updatePassword(String newPassword, String oldPassword, Integer userId) {
        Account account = accountMapper.getAccount(userId);
        if (account==null){
            return false;
        }else if(oldPassword.equals(account.getPassWord())&&(!newPassword.equals(oldPassword))){
            account.setPassWord(newPassword);
            accountMapper.updateAccount(account);
        }else{
            return false;
        }
        return false;
    }

    @Transactional
    public boolean saveMoney(Integer money, Integer userId) {

        Account account1 = accountMapper.getAccount(userId);
        if(account1==null){
            return false;
        }
        if(money>10000||money<=0){
            return false;
        }
        Record record = new Record();
        record.setUserId(userId);
        record.setMoneY(money);
        record.setType(1);
        record.setDay(new Date());
        record.setDes("人民币存款");
        recordMapper.addRecor(record);
        int money_new = account1.getBanLance() + money;
        account1.setBanLance(money_new);
        accountMapper.updateAccount(account1);
        return true;
    }

    @Transactional
    public boolean getMoney(Integer money, Integer userId) {
        //1.检查userId的账号account是否存在
        Account account2 = accountMapper.getAccount(userId);
        if(account2==null){
            return false;
        }
        //2.判断有效期
        if(System.currentTimeMillis()-account2.getValidDate().getTime()>0){
            return false;
        }
        //3.判断余额是否足够
        if(money>account2.getBanLance()||money>2000){
            return false;
        }
        //1次最多只能取2000元
        if(money>2000){
            return false;
        }
        //一天只能最多五次
        List<Record> recordslist = recordMapper.queryGetMoneyTodayRecord(userId, new java.sql.Date(System.currentTimeMillis()));
        if(recordslist.size()>=5){
            return  false;
        }
        //当天不能超过8000
        int getMoneycont=0;
        for(Record rl:recordslist){
            getMoneycont=rl.getMoneY()+getMoneycont;
        }
        if(getMoneycont>8000){
            return false;
        }
        Record record = new Record();
        //手续费
        int poundage = money/100;
        if(poundage>5){
            poundage=5;
        }
        Record pringcipalRecord = new Record();
        pringcipalRecord.setUserId(userId);
        pringcipalRecord.setMoneY(poundage);
        pringcipalRecord.setType(2);
        pringcipalRecord.setDay(new java.util.Date());
        pringcipalRecord.setDes("取钱手续费");
        recordMapper.addRecor(pringcipalRecord);

        int balance = account2.getBanLance()-money-poundage;
        account2.setBanLance(balance);
        accountMapper.updateAccount(account2);
        return true;
    }

    @Transactional
    public boolean transMoney(Integer money, Integer UserIdDest, Integer userIdmy) {
        Account account_current = accountMapper.getAccount(userIdmy);
        if(account_current==null){
            return false;
        }
        //2.判断有效期
        if(System.currentTimeMillis()-account_current.getValidDate().getTime()>0){
            return false;
        }
        Account account_test = accountMapper.getAccount(UserIdDest);
        if(account_test==null){
            return false;
        }
        //2.判断有效期
        if(System.currentTimeMillis()-account_test.getValidDate().getTime()>0){
            return false;
        }
        //手续费
        int poundage = money/100;
        if(poundage>5){
            poundage=5;
        }
        if(account_current.getBanLance()-poundage-money<0){
            System.out.println("余额不足");
            return false;
        }
        if(money>500){
            System.out.println("转账不可以多余500");
            return false;
        }
        List<Record> recordslist = recordMapper.queryTrandOutRecord(userIdmy, new java.sql.Date(System.currentTimeMillis()));
        int getMoneycont=0;
        for(Record rl:recordslist){
            getMoneycont=rl.getMoneY()+getMoneycont;
        }
        if(getMoneycont>2000){
            return false;
        }
        Record recordout= new Record();
        recordout.setMoneY(money);
        recordout.setUserId(userIdmy);
        recordout.setDay(new java.util.Date());
        recordout.setType(4);
        recordout.setDes("转出");
        recordMapper.addRecor(recordout);

        Record recordoutPoundage= new Record();
        recordoutPoundage.setMoneY(poundage);
        recordoutPoundage.setUserId(userIdmy);
        recordoutPoundage.setDay(new java.util.Date());
        recordoutPoundage.setType(5);
        recordoutPoundage.setDes("转出手续费");
        recordMapper.addRecor(recordoutPoundage);

        Record recordIn = new Record();
        recordIn.setMoneY(money);
        recordIn.setUserId(UserIdDest);
        recordIn.setDay(new java.util.Date());
        recordIn.setType(4);
        recordIn.setDes("转入");
        recordMapper.addRecor(recordIn);

        int balance = account_current.getBanLance()-money-poundage;
        account_current.setBanLance(balance);
        accountMapper.updateAccount(account_current);

        int balance1 = account_test.getBanLance()+money;
        account_test.setBanLance(balance1);
        accountMapper.updateAccount(account_test);
        return true;
    }
}
